package com.hospital.workstation.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Console;
import com.hospital.common.core.domain.R;
import com.hospital.common.utils.StringUtils;
import com.hospital.common.core.page.TableDataInfo;
import com.hospital.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hospital.workstation.domain.MedicalRecord;
import com.hospital.workstation.domain.vo.HisMedicineInfoImportVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.hospital.workstation.domain.bo.HisMedicineInfoBo;
import com.hospital.workstation.domain.vo.HisMedicineInfoVo;
import com.hospital.workstation.domain.HisMedicineInfo;
import com.hospital.workstation.mapper.HisMedicineInfoMapper;
import com.hospital.workstation.service.IHisMedicineInfoService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 药品信息Service业务层处理
 *
 * @author whb
 * @date 2023-05-24
 */
@RequiredArgsConstructor
@Service
public class HisMedicineInfoServiceImpl implements IHisMedicineInfoService {

    private final HisMedicineInfoMapper baseMapper;

    /**
     * 查询药品信息
     */
    @Override
    public HisMedicineInfoVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询药品信息列表
     */
    @Override
    public TableDataInfo<HisMedicineInfoVo> queryPageList(HisMedicineInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HisMedicineInfo> lqw = buildQueryWrapper(bo);
        Page<HisMedicineInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询药品信息列表
     */
    @Override
    public List<HisMedicineInfoVo> queryList(HisMedicineInfoBo bo) {
        LambdaQueryWrapper<HisMedicineInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HisMedicineInfo> buildQueryWrapper(HisMedicineInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HisMedicineInfo> lqw = Wrappers.lambdaQuery();
        /*lqw.eq(StringUtils.isNotBlank(bo.getMedicineCode()), HisMedicineInfo::getMedicineCode, bo.getMedicineCode());
        lqw.like(StringUtils.isNotBlank(bo.getMedicineName()), HisMedicineInfo::getMedicineName, bo.getMedicineName());
        lqw.eq(StringUtils.isNotBlank(bo.getMedicineSpell()), HisMedicineInfo::getMedicineSpell, bo.getMedicineSpell());*/
        lqw.like(StringUtils.isNotBlank(bo.getSearchValue()), HisMedicineInfo::getMedicineName, bo.getSearchValue())
            .or()
            .like(StringUtils.isNotBlank(bo.getSearchValue()), HisMedicineInfo::getMedicineSpell, bo.getSearchValue());
        return lqw;
    }

    /**
     * 新增药品信息
     */
    @Override
    public Boolean insertByBo(HisMedicineInfoBo bo) {
        HisMedicineInfo add = BeanUtil.toBean(bo, HisMedicineInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改药品信息
     */
    @Override
    public Boolean updateByBo(HisMedicineInfoBo bo) {
        HisMedicineInfo update = BeanUtil.toBean(bo, HisMedicineInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HisMedicineInfo entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除药品信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 批量保存药品信息
     *
     * @param medicineInfoImportVoList 药品信息集合
     * @return
     */
    @Override
    public R<Void> addBatchMedicalInfo(List<HisMedicineInfoImportVo> medicineInfoImportVoList) {
        List<HisMedicineInfo> medicineInfoList = new ArrayList<>();
        medicineInfoImportVoList.stream().forEach(importMedicalInfo -> {
            try {
                if (StringUtils.isNotBlank(importMedicalInfo.getMedicineSpell())) {
                    // importMedicalInfo.setMedicineSpell(importMedicalInfo.getMedicineSpell().split("/")[0]);
                    importMedicalInfo.setMedicineSpell(importMedicalInfo.getMedicineSpell().split("/")[2]);
                }
                medicineInfoList.add(BeanUtil.toBean(importMedicalInfo, HisMedicineInfo.class));
            } catch (Exception e) {
                e.printStackTrace();
                Console.log(importMedicalInfo.getMedicineCode());
            }
        });
        return baseMapper.insertBatch(medicineInfoList) ? R.ok("导入成功") : R.fail("导入失败");
    }
}
